package com.codeplay.service.impl;

import com.codeplay.entity.LearningProgress;
import com.codeplay.repository.LearningProgressRepository;
import com.codeplay.service.LearningProgressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class LearningProgressServiceImpl implements LearningProgressService {

    private final LearningProgressRepository learningProgressRepository;

    @Autowired
    public LearningProgressServiceImpl(LearningProgressRepository learningProgressRepository) {
        this.learningProgressRepository = learningProgressRepository;
    }

    @Override
    public LearningProgress createLearningProgress(LearningProgress learningProgress) {
        return learningProgressRepository.save(learningProgress);
    }

    @Override
    public LearningProgress getLearningProgressById(Long id) {
        return learningProgressRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Learning progress not found"));
    }

    @Override
    public List<LearningProgress> getLearningProgressByUserAndCourse(Long userId, Long courseId) {
        return learningProgressRepository.findByUserIdAndCourseId(userId, courseId);
    }

    @Override
    public LearningProgress getLearningProgressByUserAndContent(Long userId, Long contentId) {
        return learningProgressRepository.findByUserIdAndContentId(userId, contentId)
                .orElse(null);
    }

    @Override
    public List<LearningProgress> getLearningProgressByUserAndChapter(Long userId, Long chapterId) {
        return learningProgressRepository.findByUserIdAndChapterId(userId, chapterId);
    }

    @Override
    public LearningProgress updateLearningProgress(LearningProgress learningProgress) {
        LearningProgress existingProgress = getLearningProgressById(learningProgress.getId());
        existingProgress.setStatus(learningProgress.getStatus());
        if (learningProgress.getStatus() == LearningProgress.ProgressStatus.COMPLETED) {
            existingProgress.setCompletedAt(LocalDateTime.now());
        }
        return learningProgressRepository.save(existingProgress);
    }

    @Override
    public void deleteLearningProgress(Long id) {
        learningProgressRepository.deleteById(id);
    }

    @Override
    public double calculateCourseProgress(Long userId, Long courseId) {
        long totalContents = learningProgressRepository.countByUserIdAndCourseId(userId, courseId);
        long completedContents = learningProgressRepository.countByUserIdAndCourseIdAndStatus(
                userId, courseId, LearningProgress.ProgressStatus.COMPLETED);
        return totalContents > 0 ? (double) completedContents / totalContents * 100 : 0;
    }

    @Override
    public double calculateChapterProgress(Long userId, Long chapterId) {
        List<LearningProgress> progressList = getLearningProgressByUserAndChapter(userId, chapterId);
        if (progressList.isEmpty()) {
            return 0;
        }
        long completedContents = progressList.stream()
                .filter(p -> p.getStatus() == LearningProgress.ProgressStatus.COMPLETED)
                .count();
        return (double) completedContents / progressList.size() * 100;
    }
} 